package com.pdstudio.carrecom.ui.fragment;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.UpdateVersion;
import com.pdstudio.carrecom.common.BaseFragment;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.tools.UpdateManager;
import com.pdstudio.carrecom.ui.activity.ActivityAbout;
import com.pdstudio.carrecom.ui.activity.ActivityLogin;
import com.pdstudio.carrecom.ui.activity.more.ActivityModifierIcon;
import com.pdstudio.carrecom.ui.activity.more.ActivityModifierPassword;
import com.pdstudio.carrecom.ui.activity.more.ActivityMoreMain;
import com.pdstudio.carrecom.ui.activity.more.ActivitySuggestion;
import com.pdstudio.carrecom.ui.activity.more.ActivityUserProtocal;
import com.pdstudio.carrecom.ui.activity.more.ActivityValidePhoneAg;
import com.pdstudio.carrecom.R;

/**
 * 
 * 类: FragmentMore <br>
 * 描述: 更多界面 <br>
 * 作者: ws  <br>
 * 时间: 2015-6-5 下午9:39:54
 */
public class FragmentMore extends BaseFragment {

	private UpdateManager mUpdateManager;
	private LinearLayout mMoreUserProtocal, mMoreSuggestion, mMoreUpdate,
			mMorePhoneag,mMoreModifierPass,mIcon;
	private LinearLayout mMoreAbount,mLogout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		_view = inflater.inflate(R.layout.activity_more_main, container, false);
		_context = getActivity();
		InitialView();
		return _view;
	}

	private void InitialView() {

		mMoreUserProtocal = (LinearLayout) _view
				.findViewById(R.id.more_userprotocal);
		mMoreUserProtocal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(_context, ActivityUserProtocal.class);
				startActivity(intent);
			}
		});

		mMoreSuggestion = (LinearLayout) _view
				.findViewById(R.id.more_suggestion);
		mMoreSuggestion.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(_context, ActivitySuggestion.class);
				startActivity(intent);
			}
		});

		mMoreUpdate = (LinearLayout) _view
				.findViewById(R.id.more_updateversion);
		mMoreUpdate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					String version=_context.getPackageManager().getPackageInfo(
							"com.pdstudio.carrecom", 0).versionName;
					HttpExecuteJson http = new HttpExecuteJson(
							_context, mUpdateVersionListener);
					Map<String,Object> rps=new HashMap<String,Object>();
					rps.put("os", "android");
					rps.put("version", version);
					http.get(ServiceHelper.NewVersion, rps);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		mMoreAbount = (LinearLayout) _view.findViewById(R.id.more_about);
		mMoreAbount.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(_context, ActivityAbout.class);
				startActivity(intent);
			}
		});

		mMorePhoneag = (LinearLayout) _view.findViewById(R.id.more_phoneag);
		mMorePhoneag.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(_context,
						ActivityValidePhoneAg.class);
				startActivity(intent);
			}
		});
		
		mMoreModifierPass = (LinearLayout) _view.findViewById(R.id.more_modifierpass);
		mMoreModifierPass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(_context,
						ActivityModifierPassword.class);
				startActivity(intent);
			}
		});
		
		mLogout = (LinearLayout) _view.findViewById(R.id.more_logout);
		mLogout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AppContext.getInstance()._isLogin = false;

				AppContext.getInstance().saveLoginInfo("", "",
						false);
				AppContext.getInstance()._userId=0;
				Intent intent =new Intent(_context,ActivityLogin.class);
				startActivity(intent);
				getActivity().finish();

			}});

		mIcon=(LinearLayout)_view.findViewById(R.id.ll_more_icon);
		mIcon.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(_context,ActivityModifierIcon.class);
				startActivity(intent);
			}});
	}

	private void DoJson(String json) {
		UpdateVersion edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<UpdateVersion>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				boolean u = edv.status;
				if (u && (!StringUtils.isEmpty(edv.apkurl))) {
					mUpdateManager = new UpdateManager(_context);
					mUpdateManager.checkUpdateInfo1(edv.apkurl);
				} else {
					UIHelper.ToastMessage(_context, "当前版本已经是最新版本");
				}
			}else{
				UIHelper.ToastMessage(_context, "当前版本已经是最新版本");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private httpReturnJson mUpdateVersionListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			DoJson(result);

		}

		@Override
		public void onFailure(int error, String msg) {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(_context, "当前版本已经是最新版本");
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(_context, "当前版本已经是最新版本");
		}
	};


}
